package com.cruise.oauth.controller;



import com.cruise.common.web.result.Result;
import com.cruise.system.userapi.entity.SysUser;
import com.cruise.system.userapi.feignapi.FeignSystemApi;
import com.nimbusds.jose.jwk.JWKSet;
import com.nimbusds.jose.jwk.RSAKey;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.KeyPair;
import java.security.interfaces.RSAPublicKey;
import java.util.Map;

/**
 * 类名：PublicKeyController
 * 描述：RSA公钥开放接口
 * @author cruise
 * 日期：2021/1/2 下午11:57
 * 版本：1.0
 **/
@Api(tags = "RSA公钥开放接口")
@RestController
@AllArgsConstructor
@RequestMapping("/oauth")
public class PublicKeyController {

    @Resource
    private FeignSystemApi feignSystemApi;

    @Resource
    private KeyPair keyPair;

    @ApiOperation("获得公钥")
    @GetMapping("/getPublicKey")
    public Map<String, Object> getPublicKey() {
        RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
        RSAKey key = new RSAKey.Builder(publicKey).build();
        return new JWKSet(key).toJSONObject();
    }

    @ApiOperation("测试feign")
    @GetMapping("/testFeign")
    public Result<SysUser> testFeign() {
        Result<SysUser> admin = feignSystemApi.findByUsername("admin");
        return admin;
    }


}
